﻿Imports System.Runtime.InteropServices
Imports Microsoft.Feeds.Interop

Public Class RSSItemForm
    Dim out As Boolean
    Friend TargetLocation As Point
    Dim OldTargetLocation As Point
    Dim movex As Double
    Dim movey As Double
    Dim x As Double
    Dim y As Double
    Dim Wait As Double
    Dim gottarget As Boolean = False
    Dim RSSItem As IFeedItem

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If out = False Then
            Width = 120
            Text = ""
            MoreDetailsLabel.Visible = False
            DetailLabel.Visible = False
            VScrollBar1.Visible = False
            ContentPanel.Visible = False
            If DwmIsCompositionEnabled() Then
                Label1.Visible = False
            End If
        Else
            Label1.Visible = False
            Width = 410
            If DwmIsCompositionEnabled() Then
                DetailLabel.Visible = False
            End If
        End If
        If DwmIsCompositionEnabled() Then
            On Error Resume Next
            BackColor = Color.Black
            For Each Control As Control In Controls
                Control.BackColor = Color.Black
            Next
            VistaControls.DWM.DWMManager.EnableGlassSheet(Me)
        Else
            FormBorderStyle = Windows.Forms.FormBorderStyle.None
        End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If gottarget = False Then
            Dim Layout As New SizeF(DetailLabel.Width, -1)
            Dim Height = e.Graphics.MeasureString(DetailLabel.Text, DetailLabel.Font, Layout).Height
            DetailLabel.Height = Height
            VScrollBar1.Maximum = Height - ContentPanel.Height
            If Height < ContentPanel.Height Then : VScrollBar1.Visible = False
            Else : If out And VScrollBar1.Visible = False Then VScrollBar1.Visible = True
            End If
            If DwmIsCompositionEnabled() Then
                e.Graphics.Clear(Color.Black)
                Dim glasstxt As New GlassText()
                If out Then
                    glasstxt.DrawTextOnGlass(ContentPanel.Handle, DetailLabel.Text, DetailLabel.Font, DetailLabel.Bounds, 10)
                Else
                    glasstxt.DrawTextOnGlass(Handle, Label1.Text, Label1.Font, Label1.Bounds, 10)
                End If
            End If
        End If
    End Sub

    Private Sub LocationTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LocationTimer.Tick
        If ClosingApp = True Then
            Close()
        End If
        If Not TargetLocation = OldTargetLocation Then
            gottarget = False
        End If
        If out = True Then
            TargetLocation = New Point(My.Computer.Screen.WorkingArea.Width / 2 - (Me.Width / 2), My.Computer.Screen.WorkingArea.Height / 2 - (Me.Height / 2))
        End If
        If Not Location = TargetLocation Then
            If Wait = 0 Then
                If gottarget = False Then
                    OldTargetLocation = TargetLocation
                    x = Location.X
                    y = Location.Y
                    movex = (TargetLocation.X - Location.X) / 100
                    movey = (TargetLocation.Y - Location.Y) / 100
                    gottarget = True
                End If
                If Not Location.X = TargetLocation.X Then
                    x += movex
                    Location = New Point(x, Location.Y)
                End If
                If Not Location.Y = TargetLocation.Y Then
                    y += movey
                    Location = New Point(Location.X, y)
                End If
            Else : Wait = Math.Round(Wait - 0.01, 2)
            End If
        Else
            If gottarget = True Then
                gottarget = False
                If out Then LocationTimer.Stop()
                Invalidate()
            End If
            Wait = 0.5
        End If
    End Sub

    Private Sub Open() Handles Me.Click, Label1.Click
        If out = False Then
            Dim NewThread As New Threading.Thread(AddressOf CreateNew)
            NewThread.Start()
            Me.Close()
        End If
    End Sub

    Sub CreateNew()
        Dim NewForm As New RSSItemForm(RSSItem, Me.Location)
        NewForm.ShowDialog()
    End Sub

    Public Sub New(ByRef item As IFeedItem)
        InitializeComponent()
        out = False
        RSSItem = item
        DetailLabel.Text = GetText(item.Description)
        Label1.Text = item.Title
    End Sub

    Public Sub New(ByRef item As IFeedItem, ByVal Location As Point)
        InitializeComponent()
        ShowInTaskbar = True
        ShowIcon = True
        out = True
        RSSItem = item
        DetailLabel.Text = GetText(item.Description)
        Text = item.Title
        Me.Location = Location
        TargetLocation = Location
        Wait = 0
        MoreDetailsLabel.Visible = True
        If Not DwmIsCompositionEnabled() Then
            DetailLabel.Visible = True
            Label1.Visible = False
        End If
        FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
        MinimizeBox = True
        TopMost = False
    End Sub

    Private Sub Form_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
        If out = False Then RSSItemForms.Remove(Me)
        If out = True Then If My.Settings.DeleteItems = True Then RSSItem.Delete()
    End Sub

    Private Sub MoreDetailsLabel_LinkClicked() Handles MoreDetailsLabel.Click
        Shell("explorer.exe " & RSSItem.Link)
    End Sub

    Function GetText(ByVal htmlText As String) As String
        Dim output As String = System.Text.RegularExpressions.Regex.Replace(htmlText, "\<[^\>]+\>", "")
        output = System.Text.RegularExpressions.Regex.Replace(output, "&quot;", """"c)
        Return output
    End Function

    'Function GetImage(ByVal HTML As String) As String
    '    Dim image As String = System.Text.RegularExpressions.Regex.Match(HTML, "(\<img)(?<PreOtherParams>[^(src)]+) src=" & """"c & "(?<Source>[^" & """"c & "]+)" & """"c & "(?<PostOtherParams>[^/>]+)>").Result("${Source}")
    '    Return image
    'End Function

    Private Sub VScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles VScrollBar1.Scroll
        DetailLabel.Location = New Point(DetailLabel.Location.X, 0 - e.NewValue)
        Me.Invalidate()
    End Sub
End Class